<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Code by TinhNQ
 * 2014
 **/
class Cuisines_model extends MY_Model {

    protected $cuisine;

    function __construct(){
        parent::__construct();
    }

    function table_name(){
        return "cuisines";
    }

    function get_cuisines($filters = array(),$counting = false){
        if (isset($this->CI->cache)){
            $caching = true;
            $cache_key = 'ci--Cuisine_'.md5(serialize($filters));
            if ($counting){
                $cache_key = 'ci--Cuisine_COUNT_'.md5(serialize($filters));
            }
            if ($return = $this->cache->file->get($cache_key)) {
                return ($return == 'empty_cache') ? FALSE : $return;
            }
        }else{
            $caching = false;
        }

        $this->db->select('*');
        $this->db->from('cuisines');
        $param = array();

        if (isset($filters['cuisine_id'])) {
            $this->db->where('cuisine_id', $filters['cuisine_id']);
        }
        if (isset($filters['cuisine_id_other'])) {
            $this->db->where('cuisine_id !=', $filters['cuisine_id_other']);
        }

        $this->db->order_by('cuisine_id','DESC');

        if (isset($filters['name'])) {
            $this->db->like('name', $filters['name']);
        }
        if (isset($filters['alias'])) {
            $this->db->where('alias', $filters['alias']);
        }
        if (isset($filters['status'])) {
            $this->db->where('status', $filters['status']);
        }

        if (isset($filters['limit'])) {
            $offset = (isset($filters['page'])) ? $filters['page'] : 0;
            $this->db->limit($filters['limit'], $offset);
        }

        $result = $this->db->get();

        if($counting != false) {
            $rows = $result->num_rows();
            $result->free_result();

            if ($caching == TRUE) {
                $this->CI->cache->file->save($cache_key, $rows, (5*60));
            }

            return $rows;
        }
        if ($result->num_rows() == 0) {
            return false;
        }
        $cuisine = array();
        foreach ($result->result_array() as $row ) {
            $row['link'] = create_url('cuisine/'.$row['alias'],array('rewrite'=>true));
            $cuisine[] = $row;
        }
        if ($caching) {
            $this->CI->cache->file->save($cache_key,$cuisine);
        }
        $result->free_result();

        return $cuisine;
    }

    function get_count($filter = array()){
        return $this->get_cuisines($filter,true);
    }

    function get_cuisine($cuisineId){
        if (!isset($this->cuisine[$cuisineId])){
            $cuisine = $this->get_cuisines(array('cuisine_id'=>$cuisineId));
            $this->cuisine[$cuisineId] = $cuisine;
        }else{
            $cuisine = $this->cuisine[$cuisineId];
        }
        if (empty($cuisine)) {
            return false;
        }
        return $cuisine[0];
    }

    function insert_cuisine($data){
        $_cuisine_id = $this->save($data);
        return $_cuisine_id;
    }

    function update_cuisine($data, $cuisine_id){
        return $this->update($data, array('cuisine_id' => $cuisine_id));
    }

    function delete_cuisine($cuisine_id) {
        return $this->delete(array('cuisine_id'=>$cuisine_id));
    }

    function change_status_cuisine($cuisine_id) {
        $status = $this->db->select('status')->from('cuisines')->where('cuisine_id', $cuisine_id)->get()->result_array();

        if ($status[0]['status'] == 1) {
            $change = 0;
        } else {
            $change = 1;
        }
        return $this->update(array('status' => $change), array('cuisine_id' => $cuisine_id));
    }

    function get_cuisine_by_alias($cuisine_alias){
        $cuisine = $this->get_cuisines(array('alias'=>$cuisine_alias),false);
        if ($cuisine){
            return $cuisine[0];
        }
        return false;
    }

    function get_cuisines_options($filters=array()){
        $cuisines_options = array();
        $cuisines = $this->get_cuisines($filters);
        if (is_array($cuisines) && count($cuisines)){
            foreach ($cuisines as $cuisine){
                $cuisines_options[$cuisine['cuisine_id']] = $cuisine['name'];
            }
        }
        return $cuisines_options;
    }
	
	function get_cuisines_withtotal() {
		$sql = "SELECT c.name, c.alias, c.cuisine_id, count(1) as total 
		FROM cuisines_restaurants AS cr right Join cuisines AS c ON c.cuisine_id= cr.cuisine_id 
		Where c.status= 1 GROUP BY cuisine_id";
        $result = $this->db->query($sql);

        return $result->result_array();
    }
}
?>